//
//  ViewController.swift
//  ColorBlinds
//
//  Created by Jordi de Kock on 15-04-16.
//  Copyright © 2016 Jordi de Kock. All rights reserved.
//

import UIKit
import CoreGraphics
import QuartzCore
import AVFoundation

typealias CGGammaValue = Float
typealias CGDirectDisplayID = UInt32

@available(iOS 11.0, *)
class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
    
    var testImage: UIImageView!
    
    //MARK: - 🌟 搬运1
//    var session: AVCaptureSession!
//    var device: AVCaptureDevice!
//    var output: AVCaptureVideoDataOutput!
    //MARK: - 🌟 搬运1声明结束
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationController?.navigationBar.barTintColor = .white
        self.navigationController?.navigationBar.tintColor = .black
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
        
        self.title = "Colorful"
        
//        testImage = UIImageView.init(frame: CGRect(x: 20, y: 20, width: self.view.bounds.size.width - 40, height: 240));
        testImage = UIImageView.init(frame: CGRect(x: 20, y: 20, width: self.view.bounds.size.width - 40, height: 540));
        testImage.image = UIImage(named: "2")
        testImage.clipsToBounds = true
        testImage.contentMode = .scaleAspectFill
        self.view.addSubview(testImage);
        
        
        
        
        
        
        let nextViewButton = UIButton.init(type: .custom)
        nextViewButton.frame = CGRect(x: 20, y: testImage.frame.size.height + testImage.frame.origin.y + 20, width: self.view.bounds.size.width - 40, height: 50)
        nextViewButton.backgroundColor = #colorLiteral(red: 0.8433401585, green: 0.8512815237, blue: 0.875588119, alpha: 1)
        nextViewButton.setTitle("Next view", for: UIControl.State())
        nextViewButton.addTarget(self, action: #selector(ViewController.nextView), for: .touchUpInside)
        nextViewButton.layer.cornerRadius = 10
        self.view.addSubview(nextViewButton)
    }
    
    @objc func nextView() {
        let secondVC = SecondViewController()
        self.navigationController?.pushViewController(secondVC, animated: true)
        
        print("next view")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK: - 🌟搬运3
    func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
        // Convert a captured image buffer to UIImage.
        guard let buffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
            print("could not get a pixel buffer")
            return
        }
        CVPixelBufferLockBaseAddress(buffer, CVPixelBufferLockFlags.readOnly)
        let image = CIImage(cvPixelBuffer: buffer).oriented(CGImagePropertyOrientation.right)
        let capturedImage = UIImage(ciImage: image)
        

        // Show the result.
        DispatchQueue.main.async(execute: {
            self.testImage.image = capturedImage
        })
    }
    //MARK: - 🌟搬运3结束



}
